package com.eightbears.beboy.base.view.popupwindow;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.ShopHongBaoAdapter;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.basepopu.BasePopupWindow;
import com.eightbears.beboy.entity.ClassifyEntity;
import com.eightbears.beboy.entity.ShopHongBaoEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.socks.library.KLog;

import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_HONGBAO;

/**
 * Created by Bao on 2017/5/3.
 */
public class HongBaoPopup extends BasePopupWindow {

    private View popupView;
    private RecyclerView rl_list;
    private TextView tv_hongbao_get;
    private String shopId;
    private ShopHongBaoAdapter shopHongBaoAdapter;


    public HongBaoPopup(Activity context, String id) {
        super(context);
        this.shopId = id;
        bindEvent();
        initAdapter();
        initData();
    }

    @Override
    public Animation getAnimation() {
        return getDefaultAlphaAnimation();
    }

    @Override
    public AnimationSet getAnimationSet() {
        return null;
    }

    @Override
    public View getInputView() {
        return null;
    }

    @Override
    public View getDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.view_hongbao_popup, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {
            rl_list = (RecyclerView) popupView.findViewById(R.id.rl_classify);
            tv_hongbao_get = (TextView) popupView.findViewById(R.id.tv_hongbao_get);
            LinearLayout ll_bottom_menu = (LinearLayout) popupView.findViewById(R.id.ll_bottom_menu);
            ll_bottom_menu.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }

    private void initAdapter() {

        shopHongBaoAdapter = new ShopHongBaoAdapter();
        rl_list.setLayoutManager(new LinearLayoutManager(mContext));
        rl_list.setAdapter(shopHongBaoAdapter);
        shopHongBaoAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    private void initData() {
        String url = URL_HONGBAO + shopId + "/HB";
        OkHttpUtils.post().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("id", shopId)
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                ShopHongBaoEntity shopHongBaoEntity = GsonUtil.fromJson(response, ShopHongBaoEntity.class);
                if(shopHongBaoEntity.getData().size() == 0) {
                    tv_hongbao_get.setVisibility(View.VISIBLE);
                    rl_list.setVisibility(View.GONE);
                } else {
                    ShowToast.showShortToast("红包领取成功");
                    tv_hongbao_get.setVisibility(View.GONE);
                    rl_list.setVisibility(View.VISIBLE);
                    shopHongBaoAdapter.setNewData(shopHongBaoEntity.getData());
                }

            }

            @Override
            public void onError(Call call, Exception e) {
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(mContext, status_error);
            }
        });
    }

    public interface OnClickItemListener {
        void onClickItem(ClassifyEntity classifyEntity);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        OnClickItemListener onClicktemListener = onClickItemListener;
    }

}
